package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.model.AttentionModel;
import com.zahb.qadx.ui.view.CircleImageView;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.List;

/**
 * Created by huwei.
 * Data 2020-10-16.
 * Time 11:33.
 */
public class AttentionAdapter extends RecyclerView.Adapter<AttentionAdapter.AttentionViewHodler> {
    private Context mContext;
    private List<AttentionModel.AttentionData.AttentionList> attentionLists;
    public AttentionAdapter(Context mContext, List<AttentionModel.AttentionData.AttentionList> attentionLists) {
        this.mContext = mContext;
        this.attentionLists = attentionLists;
    }
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        //每条点击事件
        void onItemClick(View view, int position, String id);
    }
    @NonNull
    @Override
    public AttentionViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AttentionViewHodler(LayoutInflater.from(mContext).inflate(R.layout.item_attention, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AttentionViewHodler holder, int position) {
        holder.tv_title.setText(attentionLists.get(position).getTitle());
        holder.tv_name.setText(attentionLists.get(position).getName());
        final String headPhoto = attentionLists.get(position).getHeadPhoto();
                ImageLoaderKt.loadImageCircle(holder.img_pic, headPhoto);

        holder.cancel_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(view,position,String.valueOf(attentionLists.get(position).getId()));
//                Intent intent = new Intent(mContext,AttentionDetailActivity.class);
//                intent.putExtra("relationId",attentionLists.get(position).getRelationId());
//                intent.putExtra("id",attentionLists.get(position).getId());
//                intent.putExtra("name",attentionLists.get(position).getName());
//                intent.putExtra("title",attentionLists.get(position).getTitle());
//                intent.putExtra("img",attentionLists.get(position).getHeadPhoto());
//                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return attentionLists !=null ? attentionLists.size() : 0;
    }

    static class AttentionViewHodler extends RecyclerView.ViewHolder {
        private CircleImageView img_pic;
        private TextView tv_name, tv_title, cancel_attention;

        public AttentionViewHodler(@NonNull View itemView) {
            super(itemView);
            img_pic = itemView.findViewById(R.id.iv_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_title = itemView.findViewById(R.id.tv_desc);
            cancel_attention = itemView.findViewById(R.id.cancel_attention);
        }
    }
}
